import 'dart:io';
import 'package:dio/dio.dart';

var dio = new Dio(BaseOptions(
    connectTimeout: 30000, headers: {}, validateStatus: (status) => true));

class Option {
  Option({this.header, this.method, this.data, this.url}) {}
  final Map<String, String> header;
  final String method;
  final Map<String, dynamic> data;
  final String url;
}

class Request {
  Request(this.options) {}

  Future<Response<T>> request<T>() {
    switch (this.options.method) {
      case 'get':
        return fetchGet();
        break;
      case 'post':
        return fetchPost();
        break;
      default:
        return fetchGet();
    }
  }

  Future<Response<T>> fetchGet<T>() {
    return dio.get(this.options.url,
        queryParameters: this.options.data,
        options: Options(headers: this.options.header));
  }

  Future<Response<T>> fetchPost<T>() {
    return dio.post(this.options.url,
        data: this.options.data,
        options: Options(headers: this.options.header));
  }

  final Option options;
}

Future<T> request<T>(Option option) async {
  // return new Future<T>( () async => {
  Response<T> response = await Request(option).request();
  print(response.data);
  return response.data;
  // });
}
